package com.kly.user.db.dao.impl;
import com.alibaba.excel.util.StringUtils;
import com.fqgj.common.api.Page;
import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.DateUtil;
import com.google.common.collect.Maps;
import com.kly.user.db.dao.UserMessageReportDao;
import com.kly.user.db.dbo.UserMessageReportDBO;
import com.kly.user.db.entity.UserMessageReportEntity;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
* User: linqu
* Date: 2023-09-22
* Time: 17:47:36.388
*/

@Repository
public class UserMessageReportDaoImpl extends AbstractBaseMapper<UserMessageReportEntity> implements UserMessageReportDao {

    @Override
    public List<UserMessageReportEntity> queryUserMessageReport(Date beginDate) {

        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("beginDate", beginDate);
        return this.getSqlSession().selectList(this.getStatement(".queryUserMessageReport"), params);

    }

    @Override
    public List<UserMessageReportEntity> sumUserMsgReport(Date beginDate) {

        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("beginDate", beginDate);
        return this.getSqlSession().selectList(this.getStatement(".sumUserMsgReport"), params);

    }

    @Override
    public int removeTodaySumReport(Date beginDate) {

        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("beginDate", beginDate);
        return this.getSqlSession().delete(this.getStatement(".removeTodaySumReport"), params);

    }



    @Override
    public UserMessageReportEntity getByChatterUserCode(String chatterUserCode, Date beginDate) {

        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("beginDate", beginDate);
        params.put("chatterUserCode", chatterUserCode);
        return this.getSqlSession().selectOne(this.getStatement(".getByChatterUserCode"), params);

    }

    @Override
    public List<UserMessageReportDBO> getReplyReport(String chatterUserCode, String nickName, Date startDate, Date endDate, Page page) {
        final HashMap<String, Object> params = setUpParams(chatterUserCode, nickName, startDate, endDate, page);
        return this.getSqlSession().selectList(this.getStatement("getReplyReport"), params);
    }

    @Override
    public UserMessageReportDBO getReplyReportTotal(String chatterUserCode, String nickName, Date startDate, Date endDate) {

        return this.getSqlSession().selectOne(this.getStatement(".getReplyReportTotal"),
                setUpParams(chatterUserCode, nickName, startDate, endDate, null));
    }
    @Override
    public UserMessageReportDBO sumTodayReport() {
        return this.getSqlSession().selectOne(this.getStatement(".sumTodayReport"));
    }

    private HashMap<String, Object> setUpParams(String chatterUserCode, String nickName, Date startDate, Date endDate, Page page) {
        final HashMap<String, Object> params = Maps.newHashMap();
        if (startDate != null) {
            params.put("startDate", startDate);
            params.put("endDate", endDate);
        }
        if (StringUtils.isNotBlank(chatterUserCode)) {
            params.put("account", chatterUserCode);
        }
        if (StringUtils.isNotBlank(nickName)) {
            params.put("nickName", nickName);
        }
        if (page != null) {
            params.put("page", page);
        }
        return params;
    }

    @Override
    public Integer getReplyReportCnt(String chatterUserCode, String nickName, Date startDate, Date endDate, Page page) {

        return this.getSqlSession().selectOne(this.getStatement(".getReplyReportCnt"),
                setUpParams(chatterUserCode, nickName, startDate, endDate, page));
    }

    @Override
    public UserMessageReportDBO getReplyReportTotalByChatterUserCode(Integer date, String chatterUserCode) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("chatterUserCode", chatterUserCode);
        if (date != null) {
            params.put("startDate", DateUtil.getDate(date + " 00:00:00", "yyyyMMdd HH:mm:ss"));
            params.put("endDate", DateUtil.getDate(date + " 23:59:59", "yyyyMMdd HH:mm:ss"));
        } else {
            params.put("startDate", DateUtil.getTodayBeginTime());
            params.put("endDate", DateUtil.getTodayOverTime());
        }
        return this.getSqlSession().selectOne(this.getStatement("getReplyReportTotalByChatterUserCode"), params);
    }

    @Override
    public UserMessageReportDBO getContactNum(Integer date, String chatterUserCode) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("chatterUserCode", chatterUserCode);
        if (date != null) {
            params.put("startDate", DateUtil.getDate(date + " 00:00:00", "yyyyMMdd HH:mm:ss"));
            params.put("endDate", DateUtil.getDate(date + " 23:59:59", "yyyyMMdd HH:mm:ss"));
            params.put("contactDate", date);
        } else {
            params.put("startDate", DateUtil.getTodayBeginTime());
            params.put("endDate", DateUtil.getTodayOverTime());
            params.put("contactDate", Integer.valueOf(DateUtil.getDate("yyyyMMdd")));
        }
        return this.getSqlSession().selectOne(this.getStatement("getContactNum"), params);
    }
}